package com.itheima.health.service.impl;

import com.itheima.health.dao.BusinessReportDateDao;
import com.itheima.health.service.BusinessReportDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.*;


/**
 * @author ：wm
 * @date ：Created in 2022年06月09日
 * @description ：
 * @version: 1.0
 */
@Slf4j
@Service
public class BusinessReportDateServiceImpl implements BusinessReportDateService {

    @Autowired
    private BusinessReportDateDao businessReportDateDao;

    @Override
    public Map<String,Object> getBusinessReportData() {

        //定义整体接收容器
        Map<String,Object> map = new HashMap<>();

        //会员数据统计

        //定义当前日期
        Date date = new Date();
        //把当前日期转换成长整形
        Long longTime = date.getTime();
        //通过java.sql.Date方法取到yyyy-MM0dd形式的当前日期
        java.sql.Date nowDate = new java.sql.Date(longTime);
        //获取本日新增会员数
        Integer day = businessReportDateDao.getDay(nowDate);
        //从数据库查总会员数
        Integer total = businessReportDateDao.gettotal();
        //获取本周新增会员数
        Integer getwekk = businessReportDateDao.getwekk(nowDate);

        //将日期转换成字符串,声明显示格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //定义format 下面通过format获取当前的年月日
        String format = sdf.format(date);
        Integer cyear = Integer.parseInt(format.substring(0,4));
        Integer cmonth = Integer.parseInt(format.substring(5,7));
        Integer cday = Integer.parseInt(format.substring(8,10));
        //获取本月新增会员数
        Integer month = businessReportDateDao.getMonth(cyear, cmonth);

        //预约到诊数据统计

        //今日预约数
        Integer orderDay = businessReportDateDao.getorderDay(nowDate);
        //今日到诊数
        Integer getorderdz = businessReportDateDao.getorderdz(nowDate);
        //本周预约数
        Integer getorderwek = businessReportDateDao.getorderwek(nowDate);
        //本周到诊数
        Integer getorderwekdz = businessReportDateDao.getorderwekdz(nowDate);
        //本月预约数
        Integer getorderMonth = businessReportDateDao.getorderMonth(cyear, cmonth);
        //本月到诊数
        Integer getorderMonthdz = businessReportDateDao.getorderMonthdz(cyear, cmonth);


        //热门套餐

        //定义总预约人数
        long count=0;
        //定义计算当前已预约人数除以总预约人数的函数
        double v1=0;
        //获取套餐数量
        List<Map<String, Object>> setmeals = businessReportDateDao.getSetmeal();

        for (Map<String, Object> setmeal : setmeals) {
            //获取套餐值
            long value = (long)setmeal.get("value");
            //获取已预约总人数
            count+=value;
        }
        //定义List集合获取套餐map集合的值
        List list = new ArrayList();
        for (Map<String, Object> setmeal : setmeals) {
            //定义里面的map容器
            Map<String,Object> hotSetmeal = new HashMap<>();
            //获取套餐名称
            Object name = setmeal.get("name");
            //获取已预约套餐人数
            long setmeal_count = (long)setmeal.get("value");
            System.out.println(name+"------"+setmeal_count);
            //通过计算算出已预约人数所占总数的百分比
            if (count!=0){
                 v1 = (double)(setmeal_count/(count*1.0));
            }
            //对已预约占比进行格式化显示4位小数
            double proportion=(double)Math.round(v1*10000)/10000;
            hotSetmeal.put("proportion",proportion);
            hotSetmeal.put("name",name);
            hotSetmeal.put("setmeal_count",setmeal_count);
            list.add(hotSetmeal);
        }
        System.out.println(list);

        //对取得的值进行封装
        //今日新增到诊次数
        map.put("todayVisitsNumber",getorderdz);
        //当前日期
        map.put("reportDate",format);
        //今日新增会员数
        map.put("todayNewMember",day);
        //本周到诊次数
        map.put("thisWeekVisitsNumber",getorderwekdz);
        //获取热门套餐的套餐名,预约数量,占比
        map.put("hotSetmeal",list);
        //获取本月新增会员数
        map.put("thisMonthNewMember",month);
        //获取本周新增会员数
        map.put("thisWeekNewMember",getwekk);
        //获取总会员数
        map.put("totalMember",total);
        //获取该月预约次数
        map.put("thisMonthOrderNumber",getorderMonth);
        //获取该月到诊次数
        map.put("thisMonthVisitsNumber",getorderMonthdz);
        //获取今日预约次数
        map.put("todayOrderNumber",orderDay);
        //获取本周预约次数
        map.put("thisWeekOrderNumber",getorderwek);


        //封装结束返回封装的map集合
        return map;
    }
}
